<template>
	<view class="page">
		<!-- 地址 -->
		<view class="address" @click="chooseAddress">
			<view class="address-box">
				<view class="p1 flex align-center" v-if="addressData">
					<image class="width-60 height-60 margin-right-sm" :src="imgBaseUrl + 'integral/address.png'" mode="widthFix">
					</image>
					<view class="flex1">
						<view class="flex align-center ">
							<view class="label" v-if='addressData.label_name'>{{addressData.label_name}}</view>
							<view class="text1">{{ addressData.name }}</view>
							<view class="text2">{{ addressData && addressData.mobile }}</view>
						</view>
						<view class="p2 text-cut-2 text-666" v-if="addressData">
							{{ addressData.province }}{{ addressData.city }}{{ addressData.district }}{{ addressData.address }}
						</view>
					</view>
					<text class="cuIcon-right text-lg text-999"></text>
				</view>
				<view class="no-address" v-if="!addressData">{{i18n['您还没有收货地址,请先添加收货地址']}}</view>

			</view>
		</view>
		<!-- 地址 end -->
		<!-- 商品信息 -->
		<view class="goods-info bg-white">
			<!-- 具体商品  start -->
			<view class="goods-box">
				<view class="good-item" v-for="(item, index) in goods" :key="index">
					<view class="img-box">
						<image class="img" :src="item.thumb" mode="widthFix"></image>
					</view>
					<view class="right-info">
						<view>
							<view class="title">{{ item.name }}</view>
							<view class="sku text-cut">{{ item.key_name }}</view>
						</view>
						<view class="price-box">
							<view class="price flex  align-baseline" >
								<text class="text-osd  text-bold text-prices">{{ item.integral }}</text>
								<text class="  margin-left-xs text-bold text-333" style="font-size: 22rpx;"> {{i18n['积分']}}</text>
								<view class="" v-if="item.shop_price>0">
									<text class="text-black text-333 margin-lr-xs text-bold">+</text>
									<text class="text-df text-prices text-bold"> {{item.shop_price }}</text>
									<text class="text-333 text-bold"  style="font-size: 22rpx;">{{i18n['元']}}</text>
								</view>
							</view>
							<view class="num">x{{ item.number }}</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 具体商品  start -->

			<view class="info-box">
				<view class="info-item">
					<view class="item-left">
						<view class="text">{{i18n['商品总额']}}</view>
					</view>
					<view class="item-right">
						<text class=" text-df ">{{ total_integral }}</text>
						<text>积分</text>
						<text v-if="goods_price>0" class="padding-left-xs padding-right-xs">+</text>
						<text v-if="goods_price>0" class="text-price num_num">{{goods_price }}</text>
					</view>
				</view>
				<!-- 运费 -->
				<view class="info-item">
					<view class="item-left">
						<view class="text">
							<text class=" list-desc">{{i18n['运费']}}</text>
						</view>
					</view>
					<view class="item-right">
						<view class="num_num ">{{!express_price ? '￥0.00' : `￥${express_price}`}}</view>
					</view>
				</view>
				<view class="info-item">
					<view class="item-left item-left-ipt">
						<view class="text">留言</view>
						<input class="ipt" type="text" v-model="remark" maxlength="60" placeholder="建议留言前先与商家沟通确认" />
					</view>
					<view class="item-right"></view>
				</view>

				<!-- 商品合计 start -->
				<view class="info-footer solid-top padding-top-sm">
					<text class="text1 ">{{i18n['共']}}{{goods.length || 1}}{{i18n['件']}} </text>
					<text class="text1 text-bold">{{i18n['合计']}}: </text>
					<view class="text2 flex align-baseline " >
						<text class="text-bold">{{ total_integral || 0 }}</text>
						<text class=" text-bold">{{i18n['积分']}}</text>
						<text class="text-bold" v-if="payable_money>0">+</text>
						<text class="text-price text-bold"
							v-if="payable_money>0">{{ is_pure_integral ? '0.00' : payable_money }}</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 商品信息 end -->

		<!-- 底部总计和按钮 -->
		<view class="footer-box">
			<view class="left">
				<text class="text3 text-bold">{{i18n['总计']}}：</text>
				<view class="text2 flex align-baseline">
					<text class="text-bold text-df  text-bold"></text>
					<text class="text-osd  text-xxl text-bold">{{ total_integral || 0 }}</text>
					<text class="text-df  margin-left-xs text-bold">{{i18n['积分']}}</text>
					<text class=" text-bold"  v-if="payable_money>0">+</text>
					<text class="text-price text-bold"
						v-if="payable_money>0">{{ is_pure_integral ? '0.00' : payable_money }}</text>
				</view>
			</view>
			<view class="right" @click="beforeSubmit">去支付</view>
		</view>
		<!-- 底部总计和按钮 end -->


		<!-- 运费弹窗 start -->
		<fu-popup v-model="showFreight" mode="bottom" z-index="100" :safe-area-inset-bottom="true" :border-radius="24"
			ref="popupInvoice" @close="closeshowDrawer">
			<view class="freight-popup-content">
				<view class="top-box flex justify-center align-center">
					<image class="icon-title" :src="imgBaseUrl + 'integral/pop-title-left.png'" mode=""></image>
					<view class="title">{{i18n['运费说明']}}</view>
					<image class="icon-title" :src="imgBaseUrl + 'integral/pop-title-right.png'" mode=""></image>
					<image @tap="showFreight = false" class="icon-close" :src="imgBaseUrl + 'integral/close-pop.png'" mode="">
					</image>
				</view>
				<view v-if="goods&&goods[0]&&goods[0].freight_explain" class="freight-text"
					v-html='goods[0].freight_explain.replace(/\n/g, "<br/>")'>
				</view>
				<view v-else class="freight-text">
					{{i18n['无']}}
				</view>
				<view @tap="showFreight = false" class="freight-btn">
					{{i18n['确定']}}
				</view>
			</view>
		</fu-popup>
		<!-- 运费弹窗 end -->
	</view>
</template>

<script>
	import fuInvoice from '../../components/fu-invoice/fu-invoice.vue';
	import fuIntegralDeduction from '../../components/fu-integral-deduction/index.vue';
	export default {
		components: {
			fuInvoice,
			fuIntegralDeduction
		},
		data() {
			return {
				total_integral:'',//商品总额
				user_score: 0,
				is_pure_integral: 0, //是否为纯积分兑换 0 不是 1是
				sales_integral: 0, //商品积分兑换量
				goods: [], //商品信息
				addressData: null, //地址信息
				goods_price: 0, //商品价格
				express_price: 0, //是否存在运费价格
				payable_money: 0, //实付金额
				remark: '', //留言
				order: {}, //订单信息
				coupon: null, //优惠券信息
				order_type: 3, //订单类型 7预售
				isOpen: 1, //打开发票弹窗选择开不开发票
				isInvoice: 2, //默认不开发票
				show: false, //发票开启弹窗
				showFreight: false, //运费开启弹窗
				orderInfo: '', //订单信息
				invoice: {
					//填写的发票信息
					invoice_type: 1, //1个人，2企业
					invoice_title: '', //发票抬头
					invoice_company_duty_paragraph: '', //税号
					invoice_company_bank: '', //开户银行
					invoice_company_bank_num: '', //银行账号
					invoice_company_address: '', //企业地址
					invoice_company_phone: '', //企业电话
					invoice_mobile: '', //联系方式
					invoice_email: '', //电子邮件
				}
			};
		},
		filters: {
			/**
			 * @description 手机号过滤中间四位用*代替
			 * @param {Number,String} val 手机号
			 */
			filterMobile(val) {
				if (!val) return '';
				return val.substr(0, 3) + '****' + val.substr(-4);
			}
		},
		/**
		 * @description onload接收参数
		 * @param {Object} options 入参
		 */
		onLoad(options) {
			const {
				type,
				ids
			} = options;
			// 地址id
			if (options.address_id) {
				this.addressData = {
					address_id: options.address_id
				};
			}
			// this.user_score = global.userInfo.score
			this.getIntegralInfo();
			this.order = options;
			this.get_one_order_info();
			// 接收选择地址信息
			uni.$on('select_address', data => {
				this.addressData = data;
				this.get_one_order_info();
			});
			// 接收选择发票信息
			uni.$on('select_invoice', data => {
				if (data) {
					this.invoice = data;
					if (data.invoice_title) {
						this.isInvoice = 1;
					} else {
						this.isInvoice = 2;
					}
				} else {
					this.isInvoice = 2;
				}
			});
		},
		onUnload() {
			// 关闭监听
			uni.$off('select_address');
			uni.$off('select_invoice');
		},
		computed: {

		},
		methods: {
			/**
			 * @description 获取积分统计
			 */
			getIntegralInfo() {
				this.$api
					.get(global.apiUrls.getMyMoney)
					.then((res) => {
						if (res.data.code == 1) {
							this.user_score = res.data.data.score;
							this.loadData();
						} else {
							this.$message.info(res.data.msg);
						}
					})
					.catch((err) => {
						console.log("err: " + JSON.stringify(err));
					});
			},
			/**
			 * 跳转到发票页面
			 */
			openInvoice() {
				let _this = this;
				this.$urouter.navigateTo({
					url: '/pages/order/order-process/choose_invoice/index',
					params: _this.invoice
				});
			},
			/**
			 * @description 关闭发票弹窗
			 */
			closeshowDrawer() {
				this.show = false;
				// 关闭发票弹窗时判断 若选择了开发票且发票信息填写了则是开发票  否者是不开发票
				if (this.invoice.invoice_title && this.isOpen == 1) {
					this.isInvoice = 1;
				} else {
					this.isInvoice = 2;
				}
			},
			/**
			 * @description 非购物车页面进来获取订单详情
			 */
			get_one_order_info() {
				const {
					goods_id,
					sku_id,
					number,
					activity_id = 0
				} = this.order;
				const _data = {
					goods_id: goods_id,
					sku_id: sku_id || 0,
					number: number,
					coupon_id: '0',
					address_id: (this.addressData && this.addressData.address_id) || 0,
					activity_id: activity_id
				};

				this.$api.post(global.apiUrls.postBuyNowOrderInfos, _data).then(res => {
					if (res.data.code == 1) {
						let data = res.data.data;
						if (data.address && !Array.isArray(data.address)) {
							this.addressData = data.address;
						}
						this.orderInfo = res.data.data
						this.goods = [data.goods_integral];
						this.is_pure_integral = data.is_pure_integral;
						this.sales_integral = data.integral_reduce;
						this.goods_price = data.goods_price;
						this.express_price = data.express_price;
						this.payable_money = data.payable_money;
						this.total_integral = data.total_integral
						if (data.order_type) {
							this.order_type = data.order_type;
						}

					} else {
						this.$message.info(res.data.msg);
						setTimeout(function() {
							uni.navigateBack();
						}, 800);
					}
					uni.stopPullDownRefresh();
				});
			},

			/**
			 * @description 选择地址
			 */
			chooseAddress() {
				this.$urouter.navigateTo({
					url: '/pages/user/address/address-list/index',
					params: {
						type: 1,
						id: (this.addressData && this.addressData.address_id) || 0
					}
				});
			},

			/**
			 * @description 提交订单之前验证
			 */
			beforeSubmit() {
				this.$util.throttle(() => {
					this.handleSubmit()
				}, 2000, true);
			},
			/**
			 * @description 确认订单 下单
			 */
			handleSubmit() {
				const that = this;
				var order_info = {};
				order_info.goods = this.goods;
				order_info.express_price = this.express_price;
				order_info.remark = this.remark;
				order_info.address = this.addressData;
				var postdata = {
					goods_id: this.goods[0].id,
					address_id: this.addressData.address_id,
					remark:this.remark ,
          number:this.goods[0].number,
          payable_money: this.payable_money
				};
				// 邀请码
				if (this.order.invite_code) {
					postdata.share_sign = this.order.invite_code;
				} 
				this.$api.post(global.apiUrls.postCreateOrders, postdata).then(res => {
					if (res.data.code == 1) {
						if (this.payable_money <= 0) {
							this.$message.info(this.i18n['积分兑换中，请稍等…']);
              this.zeroPay(res.data.data.order_sn)
						} else {
							this.$message.info(this.i18n['订单提交成功，请支付…']);
              setTimeout(() => {
              	// 积分 + 钱 兑换 需要去支付金额
              	this.$urouter.redirectTo({
              		url: '/pages/integral/integral-order-process/pay-order/index',
              		params: {
              			order_sn: res.data.data.order_sn,
              			order_type: this.order_type
              		}
              	});
              }, 500);
						}
						// if (res.data.data.is_pay == 1) {
						// } else {

						// }
					} else {
						this.$message.info(res.data.msg);
					}
				});
			},
			// 0元购
			zeroPay(order_sn) {
        setTimeout(() => {
        	uni.redirectTo({
        		url: '/pages/integral/integralgoods/exchange-success/index'
        	});
        }, 500);
				// this.$api.post(global.apiUrls.postZeroOrderPay, {
				// 	order_sn: order_sn
				// }).then(res => {
				// 	if (res.data.code == 1) {
				// 	} else {
				// 		this.$message.info(res.data.msg);
				// 	}
				// }).catch(err => {
				// 	this.$message.info(res.data.msg);
				// 	this.loading = false;
				// })
			},
		},
		onPullDownRefresh() {
			this.get_one_order_info();
		}
	};
</script>

<style lang="scss" scoped>
	.page {
		padding-bottom: 10rpx;
	}

	// 地址
	.address {
		margin: 24rpx auto 0;
		// width: 702rpx;
		border-radius: 16rpx;
		display: flex;
		align-items: center;
		background: #fff url($IMG_BASE_URL + 'integral/bottom_line.png') -7px bottom repeat-x;
		background-size: 550rpx 6rpx;
		padding: 30rpx 0;

		.address-box {
			padding: 10rpx 30rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			width: 100%;
			flex-shrink: 0;
			.p1 {
				position: relative;
				.label {
					min-width: 56rpx;
					padding: 0 10rpx;
					height: 32rpx;
					background: rgba(105, 139, 255, 0.08);
					border: 1px solid #698bff;
					opacity: 1;
					border-radius: 8rpx;
					text-align: center;
					font-size: 24rpx;
					font-weight: 500;
					line-height: 32rpx;
					color: #698bff;
				}

				.text1 {
					margin-left: 16rpx;
					font-size: 32rpx;
					font-weight: bold;
					line-height: 34rpx;
					color: #333333;
				}

				.text2 {
					margin-left: 32rpx;
					font-size: 32rpx;
					font-family: Lato;
					font-weight: 500;
					line-height: 34rpx;
					color: #333333;
				}
			}

			.p2 {
				margin-top: 16rpx;
				line-height: 40rpx;

			}

			.no-address {
				color: #999;
				font-size: 30rpx;
			}
		}

	}

	// 商品信息
	.goods-info {
		margin: 20rpx auto 122rpx;
		width: 702rpx;
		overflow: hidden;
		border-radius: 16rpx;
		.goods-box {
			position: relative;
			width: 654rpx;
			margin: 0 auto;
			overflow: hidden;
			border-bottom: 1px solid #eeeeee;
			opacity: 1;
			padding-bottom: 32rpx;
			height: auto;

			.good-item {
				width: 654rpx;
				height: 200rpx;
				margin: 0 auto;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 32rpx;

				.img-box {
					width: 200rpx;
					height: 200rpx;
					flex-shrink: 0;
					position: relative;
					border-radius: 16rpx;
					overflow: hidden;

					.img {
						position: absolute;
						left: 50%;
						top: 50%;
						transform: translateX(-50%) translateY(-50%);
						width: 200rpx;
					}
				}

				.right-info {
					width: 430rpx;
					height: 200rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.title {
						font-family: PingFang SC;
						font-weight: 500;
						line-height: 42rpx;
						color: #333333;
						//两行溢出隐藏
						white-space: normal;
						word-break: break-all;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
					}

					.sku {
						margin-top: 8rpx;
						font-size: 24rpx;
						font-weight: 400;
						line-height: 33rpx;
						color: #999999;
	
					}

					.price-box {
						width: 100%;
						display: flex;
						justify-content: space-between;
						align-items: center;
						.price {
							font-size: 32rpx;
							font-family: Oswald;
							font-weight: 500;
							line-height: 36rpx;
							color: #fa2033;

						}

						.num {
							font-size: 24rpx;
							line-height: 33rpx;
							color: #999999;
						}
					}
				}
			}
		}

		.info-box {
			width: 654rpx;
			margin: 0 auto;
			overflow: hidden;

			.info-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 32rpx;

				.item-left-ipt {
					width: 100%;
				}

				.item-left {
					display: flex;
					align-items: center;

					// 商品总额 优惠券 运费
					.text {
						font-family: PingFang SC;
						font-weight: 500;
						line-height: 42rpx;
						color: #333333;
					}

					// 全场满88免运费
					.text2 {
						margin-left: 32rpx;
						line-height: 42rpx;
						color: #bfbfbf;
					}

					.ipt {
						padding-left: 32rpx;
						line-height: 42rpx;
						color: #bfbfbf;
						opacity: 1;
						width: 90%;
					}
				}

				.item-right {
					display: flex;
					align-items: center;
					font-weight: 500;
					line-height: 42rpx;

					.icon {
						display: block;
						width: 12rpx;
						height: 24rpx;
						flex-shrink: 0;
					}
				}
			}
		}

		.info-footer {
			width: 654rpx;
			margin: 45rpx auto 32rpx;
			overflow: hidden;
			display: flex;
			justify-content: flex-end;

			.text1 {
				margin-right: 8rpx;
				font-size: 24rpx;
				font-weight: 500;
				line-height: 42rpx;
				color: #666666;
			}

			.text3 {
				color: #333333;
			}

			.text2 {
				font-size: 32rpx;
				font-family: Oswald;
				font-weight: 500;
				line-height: 36rpx;
				color: #fa2033;
				opacity: 1;

				.small {
					font-size: 22rpx;
				}
			}
		}
	}

	// 底部总计和按钮
	.footer-box {
		height: 90rpx;
		height: calc(90rpx + constant(safe-area-inset-bottom));
		height: calc(90rpx + env(safe-area-inset-bottom));
		width: 750rpx;
		left: 0;
		position: fixed;
		bottom: 0 !important;
		z-index: 99;
		background: #ffffff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);


		.left {
			display: flex;
			align-items: center;
			margin-left: 32rpx;

			.text1 {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 32rpx;
				color: #1a1a1a;
				opacity: 1;
			}

			.text2 {
				font-size: 32rpx;
				font-family: Oswald;
				font-weight: 500;
				line-height: 36rpx;
				color: #fa2033;
				opacity: 1;

				.small {
					font-size: 22rpx;
				}
			}
		}

		.right {
			width: 280rpx;
			// height: 90rpx;
			background: $theme;
			opacity: 1;
			text-align: center;
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			line-height: 90rpx;
			color: #ffffff;
			opacity: 1;
		}
	}

	.freight-popup-content {
		.freight-title {
			padding-left: 32rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #333333;
		}

		.freight-text {
			padding: 16rpx 32rpx 57rpx 32rpx;
			color: #666666;
		}

		.freight-btn {
			width: 750rpx;
			height: 100rpx;
			background: linear-gradient(126deg, #FA2033 0%, #FF6530 100%);
			text-align: center;
			font-size: 32rpx;
			line-height: 100rpx;
			color: #FFFFFF;

		}
		.top-box {
			position: relative;
			padding-top: 40rpx;

			.icon-title {
				width: 32rpx;
				height: 10rpx;
			}

			.title {
				font-size: 36rpx;
				font-weight: bold;
				color: #333333;
				margin-left: 24rpx;
				margin-right: 24rpx;
			}

			.icon-close {
				position: absolute;
				right: 32rpx;
				top: 50rpx;
				height: 28rpx;
				width: 28rpx;
			}
		}
	}




	.list-desc {
		line-height: 30rpx;
		line-height: 42rpx;
		color: #333333;
	}

	.num_num {

		height: 40rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		line-height: 42rpx;
		color: #333333;
	}

	// 积分弹框 end
	/deep/ uni-switch.checked-green .uni-switch-input {
		background-color: #39b54a !important;
		border-color: #39b54a !important;
		color: #ffffff !important;
	}
</style>
